local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "roomsvrhelper"
local msgproxy = require "msgproxy"
local base = require "base"
local configdao = require "configdao"
local json = require "cjson"
local timetool = require "timetool"
local tabletool = require "tabletool"

json.encode_sparse_array(true,1,1)
require "enum"

local filename = "RoomsvrRequest.lua"
local RoomsvrRequest = {}

function RoomsvrRequest.process(session, source, event, ...)
	local f = RoomsvrRequest[event] 
	if f == nil then
		filelog.sys_error(filename.." RoomsvrRequest.process invalid event:"..event)
		base.skynet_retpack(nil)
		return
	end
	f(session, source, ...)
end

--[[
//请求创建朋友桌
message CreateFriendTableReq {
	optional Version version = 1;
	optional int32   gameid = 2;
	optional string  password = 3;  //密码 如果没有密码填空串
	optional int32   room_type = 4;   //创建房间模式, 2表示自建房卡场 3表示自建金币场
	optional int32   retain_to_num = 5;//创建房间局数
	optional int32   roomcardnum = 6;  //消耗房卡数
	optional string  name = 7;
	optional string  gamecfg = 8;     //创建房间配置 json串，格式和没有游戏协议中的CreateTableCfg对应
}

//响应创建朋友桌
message CreateFriendTableRes {
	optional int32  errcode = 1; //错误原因 0表示成功
	optional string errcodedes = 2; //错误描述
	optional string create_table_id = 3; //朋友桌索引号
	optional int32  gameid = 4;  //游戏类型id 
}
]]

function RoomsvrRequest.createfriendtable(session, source, request)
	local responsemsg = {
		errcode = EErrCode.ERR_SUCCESS,
	}

	if request == nil or request.gameid == nil or request.gamecfg == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求！"
		base.skynet_retpack(responsemsg)
		filelog.sys_error("RoomsvrRequest.createfriendtable two invalid request param")
		return
	end

	local createtablecfg = msghelper:get_createtable_conf(request.room_type)
	if createtablecfg == nil then
		responsemsg.errcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.errcodedes = "无效请求, 创建房间的room_type无效"
		base.skynet_retpack(responsemsg)
		filelog.sys_error("RoomsvrRequest.createfriendtable two invalid room_type")
		return
	end

	local conf = tabletool.deepcopy(createtablecfg)
	conf.name = request.name or request.playerinfo.rolename.."的房间"
    conf.create_user_rid = request.rid
    conf.create_user_rolename = request.playerinfo.rolename
    conf.create_user_logo = request.playerinfo.logo
    conf.create_time = timetool.get_time()
    conf.roomcardnum = request.roomcardnum
    conf.password = request.password
    conf.is_public = request.is_public
    conf.retain_to_num = request.retain_to_num
    conf.retain_time = configdao.get_businessconfitem_by_index(100, 1000, "globalvarcfg", "max_createtable_idletime")
    conf.gamecfg = json.decode(request.gamecfg)
    conf.gameid = request.gameid

    local result, create_table_id = msghelper:create_friend_table(conf)
    if not result then
		responsemsg.errcode = EErrCode.ERR_CREATE_TABLE_FAILED
    	responsemsg.errcodedes = "系统错误，创建朋友桌失败！"
		filelog.sys_error("RoomsvrRequest.createfriendtable create_friend_table failed")
    else
    	responsemsg.create_table_id = create_table_id
    end

    base.skynet_retpack(responsemsg)
end

function RoomsvrRequest.gmcmd(cmd, ... )
	if cmd == "reload" then
		msghelper:event_process("lua", "cmd", "reload", ...)
		return
	end
end

return RoomsvrRequest